﻿package com.demo.client;

import android.content.Context;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.preference.PreferenceScreen;
import android.preference.Preference.OnPreferenceChangeListener;
import android.util.Log;




/**
 * activity for display the notification setting view
 * 
 * @author Administrator
 *
 */
public class NotificationSettingsActivity extends PreferenceActivity{

	private static final String TAG = LogUtil.getClassName(NotificationSettingsActivity.class);
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setPreferenceScreen(createPreferenceScreen());
		
		setNotificationDependencies();
		CheckBoxPreference checkBoxPre = (CheckBoxPreference) getPreferenceManager().findPreference(
				Constants.SET_NOTIFICATION_ENABLE);
		
		if(checkBoxPre.isChecked())
		{
			checkBoxPre.setTitle("notification is enable");
		}else
		{
			checkBoxPre.setTitle("notification is disable");
		}
	}
	
	private PreferenceScreen createPreferenceScreen()
	{
		Log.d(TAG, "createPreferenceScreen...");
		
		PreferenceManager preManager = getPreferenceManager();
		preManager.setSharedPreferencesName(Constants.SHARE_PREFERENCE_NAME);
		preManager.setSharedPreferencesMode(Context.MODE_PRIVATE);
		
		PreferenceScreen preScreen = preManager.createPreferenceScreen(this);
		
		CheckBoxPreference checkBoxPre = new CheckBoxPreference(this);
		checkBoxPre.setKey(Constants.SET_NOTIFICATION_ENABLE);
		checkBoxPre.setDefaultValue(Boolean.TRUE);
		checkBoxPre.setTitle("notificatoin is enable");
		checkBoxPre.setSummaryOff("do not receive message~");
		checkBoxPre.setSummaryOn("receive message~");
		checkBoxPre.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
			@Override
			public boolean onPreferenceChange(Preference preference, Object newValue) {
				boolean isChecked = Boolean.valueOf(newValue.toString());
				
				if(isChecked)
					preference.setTitle("notification is enable");
				else
					preference.setTitle("notification is disable");
				return true;
			}
		});
		//铃声
		CheckBoxPreference ringPre = new CheckBoxPreference(this);
		ringPre.setKey(Constants.SET_SOUND_ENABLE);
		ringPre.setDefaultValue(Boolean.TRUE);
		ringPre.setTitle("Sound");
		ringPre.setSummary("playt sound for notification");
		
		//震动
		CheckBoxPreference vibratePre = new CheckBoxPreference(this);
		vibratePre.setKey(Constants.SET_Vibrate_ENABLE);
		vibratePre.setDefaultValue(Boolean.TRUE);
		vibratePre.setTitle("Vibrate");
		vibratePre.setSummary("set the vibrate for notification");
		
		preScreen.addPreference(checkBoxPre);
		preScreen.addPreference(ringPre);
		preScreen.addPreference(vibratePre);
		
		return preScreen;
	}
	
	/**
	 * 设置sound 和vibrator的依靠项
	 * 
	 */
	private void setNotificationDependencies()
	{
		Preference ringPre = getPreferenceManager().findPreference(Constants.SET_SOUND_ENABLE);
		if(ringPre!=null)
			ringPre.setDependency(Constants.SET_NOTIFICATION_ENABLE);
		
		Preference vibratorPre = getPreferenceManager().findPreference(Constants.SET_Vibrate_ENABLE);
		if(vibratorPre!=null)
			vibratorPre.setDependency(Constants.SET_NOTIFICATION_ENABLE);
	}
}
